<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登陆页面</title>
    <link rel="stylesheet" href="./css/login.css">
    <link rel="stylesheet" href="./css/common.css">

    <script src="./js/jQuery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="./img/logo.png" alt="">
        <p>闲聊屋子</p>

    </div>
    <div class="container">
        <p>登录</p>
        <div class="login"><span>用户名</span><input type="text" id="username" placeholder="请输入用户名"></div>
        <div class="login"><span>密码&nbsp;&nbsp;&nbsp;&nbsp;</span>
            <input type="password" id="password" placeholder="请输入密码">
        </div>
        <div class="login"><span>验证码</span>
            <input type="text" id="vr_code_pre" placeholder="请输入验证码">
            <img src="user/captcha" alt="验证码" id="vr_code" onclick="changeCode()">
        </div>


        <div class="btn">
            <button id="sub" type="submit" onclick="login()">登录</button>
            <button id="reg" type="submit" onclick="register()">注册</button>
        </div>
    </div>
    <script>
        function login() {
            var $username = jQuery('#username')
            var $password = jQuery('#password')
            var $vrCode = jQuery('#vr_code_pre')
            if ($username.val() == '') {
                alert('请输入用户名！')
                $username.focus()
                return
            }
            if ($password.val() == '') {
                alert('请输入密码！')
                $password.focus()
                return
            }
            if ($vrCode.val() == '') {
                alert('请输入验证码！')
                $vrCode.focus()
                return
            }
            jQuery.ajax({
                url: "user/login",
                type: "post",
                data: {
                    "username": $username.val(),
                    "password": $password.val(),
                    "captcha": $vrCode.val()
                },
                success: function (result) {
                    if (result.code == 200) {
                        alert('登陆成功！')
                        location.href = "./client.html"
                    } else {
                        alert(result.msg)
                    }
                    changeCode()    //更新验证码
                }
            })
        }
        function changeCode() {
            jQuery('#vr_code').attr('src', 'user/captcha?' + Math.random())
        }

        function register() {
            location.href = "../../../../../blog_system_ssm/src/main/resources/static/reg.html"
        }
    </script>
</body>

</html>